const gulp = require('gulp');
const shell = require('gulp-shell');
const sequence = require('gulp-sequence');


gulp.task('clean', () => {
	const del = require('del');
	return del('./dist/');
});

gulp.task('tslint', shell.task('tslint -p tsconfig.json'));

gulp.task('compile-ts', shell.task('tsc'));

gulp.task('copy-files', () => {
	return gulp.src(['./package.json', './feidao.json', './log4js.json', './readme.md'])
		.pipe(gulp.dest('./dist/'));
});

gulp.task('compile-ts', shell.task('tsc'));

gulp.task('default', sequence('clean', 'tslint', ['copy-files', 'compile-ts']));

gulp.task('docs-atom',
		['clean'],
		() => {
			const dest = './docs';
			const path = require('path');
			const through = require('through2');
			return gulp.src([`./src/atom/**/readme.md`])
				.pipe(through.obj((file, enc, callback) => {
					const dir_path = path.dirname(file.path);
					const file_path = dir_path.substring(dir_path.indexOf('atom'));
					const out = dest + file_path.substring(4);
					const name = path.basename(path.dirname(file.path));
					const task = 'typedoc --out ' + out + ' --excludeExternals --excludePrivate --readme ./src/' + file_path + '/readme.md --name ' + name + ' --exclude !**/atom/** ./src/' + file_path + '/';
					callback(null, task);
				}))
				.pipe(shell('<%= file %>'));
		}
	);